﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace XDL.Framework.Screens
{
    public class DialogMenuScreen :
        MenuScreen
    {
        Texture2D m_cornerBoxTexture;
        Rectangle m_menuArea = new Rectangle();

        public override Rectangle GetDrawArea()
        {
            return m_menuArea;
        }

        public override void LoadContent()
        {
            base.LoadContent();
            // border UI
            m_cornerBoxTexture = GameServices.LoadContent<Texture2D>("2d/Misc/CornerBox");
        }

        public override void Draw(GameTime gameTime)
        {
            // get menu box size
            Vector2 textSize = Vector2.Zero;
            foreach (UIElement it in Controls)
            {
                Vector2 menuTextSize = it.Measure();
                textSize.X = Math.Max(textSize.X, menuTextSize.X);
                textSize.Y += menuTextSize.Y;
            }
            System.Diagnostics.Debug.Assert(textSize.LengthSquared() > 0);

            // Center the message text in the viewport.
            Viewport viewport = GameServices.Viewport;
            Vector2 textPosition = new Vector2(
                viewport.TitleSafeArea.X + viewport.TitleSafeArea.Width / 2 - textSize.X / 2,
                viewport.TitleSafeArea.Y + viewport.TitleSafeArea.Height / 2 - textSize.Y);

            // update "inside"
            m_menuArea = new Rectangle(
             (int)(textPosition.X),
             (int)(textPosition.Y),
             (int)(textSize.X),
             (int)(textSize.Y)); // not used anyway

            // give some 'room' to the text (borders)
            Rectangle rect = m_menuArea;
            rect.X -= m_cornerBoxTexture.Width;
            rect.Y -= m_cornerBoxTexture.Height;
            rect.Width += m_cornerBoxTexture.Width;
            rect.Height += m_cornerBoxTexture.Height;

            // background box
            Color boxColor = new Color(63, 63, 63, (byte)(128.0f * (float)TransitionAlpha / 255.0f));
            ScreenManager.DrawRectangle(rect, boxColor);

            // Fade the text alpha during transitions.
            Color textColor = new Color(TextColor.R, TextColor.G, TextColor.B, TransitionAlpha);

            ScreenManager.SpriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
            // top/left
            ScreenManager.SpriteBatch.Draw(
                m_cornerBoxTexture,
                new Rectangle(
                    rect.X - m_cornerBoxTexture.Width + 1,
                    rect.Y - m_cornerBoxTexture.Height + 1,
                    m_cornerBoxTexture.Width - 1,
                    m_cornerBoxTexture.Height - 1),
                new Rectangle(0, 0, m_cornerBoxTexture.Width - 1, m_cornerBoxTexture.Height - 1),
                textColor);
            // top
            ScreenManager.SpriteBatch.Draw(
                m_cornerBoxTexture,
                new Rectangle(
                    rect.X,
                    rect.Y - m_cornerBoxTexture.Height + 1,
                    rect.Width,
                    m_cornerBoxTexture.Height - 1),
                new Rectangle(m_cornerBoxTexture.Width - 1, 0, 1, m_cornerBoxTexture.Height - 1),
                textColor);
            // top/right
            ScreenManager.SpriteBatch.Draw(
                m_cornerBoxTexture,
                new Rectangle(
                    rect.X + rect.Width,
                    rect.Y - m_cornerBoxTexture.Height + 1,
                    m_cornerBoxTexture.Width - 1,
                    m_cornerBoxTexture.Height - 1),
                new Rectangle(0, 0, m_cornerBoxTexture.Width - 1, m_cornerBoxTexture.Height - 1),
                textColor,
                0,
                Vector2.Zero,
                SpriteEffects.FlipHorizontally,
                0);

            // bottom/left
            ScreenManager.SpriteBatch.Draw(
                m_cornerBoxTexture,
                new Rectangle(
                    rect.X - m_cornerBoxTexture.Width + 1,
                    rect.Y + rect.Height,
                    m_cornerBoxTexture.Width - 1,
                    m_cornerBoxTexture.Height - 1),
                new Rectangle(0, 0, m_cornerBoxTexture.Width - 1, m_cornerBoxTexture.Height - 1),
                textColor,
                0,
                Vector2.Zero,
                SpriteEffects.FlipVertically, 0);
            // bottom
            ScreenManager.SpriteBatch.Draw(
                m_cornerBoxTexture,
                new Rectangle(
                    rect.X,
                    rect.Y + rect.Height,
                    rect.Width,
                    m_cornerBoxTexture.Height - 1),
                new Rectangle(m_cornerBoxTexture.Width - 1, 0, 1, m_cornerBoxTexture.Height - 1),
                textColor,
                0,
                Vector2.Zero,
                SpriteEffects.FlipVertically, 0);
            // bottom/right
            ScreenManager.SpriteBatch.Draw(
                m_cornerBoxTexture,
                new Rectangle(
                    rect.X + rect.Width,
                    rect.Y + rect.Height,
                    m_cornerBoxTexture.Width - 1,
                    m_cornerBoxTexture.Height - 1),
                new Rectangle(0, 0, m_cornerBoxTexture.Width - 1, m_cornerBoxTexture.Height - 1),
                textColor,
                2.0f * MathHelper.PiOver2,
                new Vector2(m_cornerBoxTexture.Width - 1, m_cornerBoxTexture.Height - 1),
                SpriteEffects.None,
                0);

            // left
            ScreenManager.SpriteBatch.Draw(
                m_cornerBoxTexture,
                new Rectangle(
                    rect.X - m_cornerBoxTexture.Width + 1,
                    rect.Y,
                    m_cornerBoxTexture.Width - 1,
                    rect.Height),
                new Rectangle(0, m_cornerBoxTexture.Height - 1, m_cornerBoxTexture.Width - 1, 1),
                textColor);
            // right
            ScreenManager.SpriteBatch.Draw(
                m_cornerBoxTexture,
                new Rectangle(
                    rect.X + rect.Width,
                    rect.Y,
                    m_cornerBoxTexture.Width - 1,
                    rect.Height),
                new Rectangle(0, m_cornerBoxTexture.Height - 1, m_cornerBoxTexture.Width - 1, 1),
                textColor,
                0,
                Vector2.Zero,
                SpriteEffects.FlipHorizontally, 0);

            ScreenManager.SpriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
